/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.future.handler;

import android.content.Context;

import com.ouertech.android.agm.lib.base.bean.BaseRequest;
import com.ouertech.android.agm.lib.base.future.http.HttpEvent;
import com.ouertech.android.agm.lib.base.utils.UtilMD5;
import com.simon.mengkou.data.bean.base.PhoneAccessToken;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.bean.req.LoginReq;
import com.simon.mengkou.data.bean.req.RegisterReq;
import com.simon.mengkou.data.cache.DataCacheImpl;
import com.simon.mengkou.data.db.dao.UserDao;
import com.simon.mengkou.data.enums.EAccountType;
import com.simon.mengkou.future.base.OuerFutureData;
import com.simon.mengkou.future.base.OuerHttpHandler;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.xiaomi.mipush.sdk.MiPushClient;


/**
 * @author Wang
 * @date 16/2/23
 * @desc 登录、注册请求业务逻辑
 */
public class LoginHandler extends OuerHttpHandler {
    private String mPhone;
    private String mPassword;
    private boolean mFlag;

    public LoginHandler(Context context) {
        super(context);
        mFlag = false;
    }

    @Override
    public boolean onEncode(HttpEvent evt) throws Exception {
        OuerFutureData futureData = (OuerFutureData)evt.getData();
        BaseRequest req =futureData.getReq();
        if(req instanceof  LoginReq) {//登录请求
            LoginReq loginReq = (LoginReq) req;
            if(loginReq.getType() == EAccountType.PHONE) {
                mFlag = true;
                mPhone = loginReq.getPhone();
                mPassword = loginReq.getPassWord();
            }
        } else if(req instanceof RegisterReq) {//注册请求
            RegisterReq registerReq = (RegisterReq) req;
            mFlag = true;
            mPhone = registerReq.getPhone();
            mPassword = registerReq.getPasswd();
        }

        return super.onEncode(evt);
    }

    @Override
    public void onHandle(HttpEvent evt) throws Exception {
        User user = (User) evt.getData();

        OuerApplication.mUser = user;
        //保存当前的用户id
        OuerApplication.mPreferences.setUserId(user.getUserId());
        //保存当前的用户信息到数据库
        UserDao userDao = OuerApplication.mDaoFactory.getUserDao();
        userDao.addUser(user);

        //保存手机号码、密码
        if(mFlag) {
            PhoneAccessToken token = new PhoneAccessToken();
            token.setPhone(mPhone);
            token.setPassword(mPassword);
            PhoneAccessToken.writeAccessToken(mContext, token);
        }

        //保存默认收货地址
        if(user.getAddress() != null) {
            OuerApplication.mCacheFactory.getCache(DataCacheImpl.AddressDefaultCache.class, user.getUserId())
                    .save(user.getAddress());
        }

        //设置推送别名
        MiPushClient.setAlias(mContext, user.getAlias(), null);
        MiPushClient.setUserAccount(mContext, user.getUserId(), null);

        OuerDispatcher.sendLoginedBroadcast(mContext);
        evt.getFuture().commitComplete(user);
    }
}
